import { getResources } from "./menuResource";

/**
 * @param {Array} value
 * @returns {Boolean}
 * @example see @/views/permission/directive.vue
 */
export const checkButtonPermission = (buttonCode,pageCode)=> {
  let resources = getResources();
  if (!pageCode) {
    return false;
  }

  if (!buttonCode) {
    return false;
  }

  let pages = resources.filter(item=>item.code==pageCode&& item.type==0);
  if (!pages) {
    return false;
  }

  let buttons = [];
  for (let key in pages) {

    buttons = resources.filter(item=>item.type==1&&item.code==buttonCode&&item.pid==pages[key].id);
    if (buttons.length>0) {
      return true;
    }
  }

  return false;
}
const baseUri = process.env.VUE_APP_ROUTER_BASE

export const checkPagePermission = (pageUri) => {
  
  let resources = getResources();
  if (!Array.isArray(resources)) {
    resources = []
  }

  if (!pageUri) {
    return false;
  }

  // let pages = resources.filter(item=>item.uri==pageCode && item.type==0);
  // console.log(resources)
  let pages = resources.filter(item=>{
    // console.log('=============')
    // console.log(`${baseUri}${pageUri}`)
    return item.uri === `${baseUri}${pageUri}` && item.type === 1
  });

  if (pages.length<1) {
    return false;
  }
  return true;
}
